package bairui.international.console.web.controller.personnel.position.employee;

import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianjing.sh.spring.web.controller.base.action.Action;
import com.lianjing.sh.spring.web.controller.enhance.output.FomatOutput;

import bairui.international.model.domain.jpa.TB_Admin;
import bairui.international.model.domain.jpa.TB_Department;
import bairui.international.model.domain.jpa.TB_Position;
import bairui.international.service.Service_Admin;
import bairui.international.service.Service_Department;
import bairui.international.service.Service_Position;


/**
 * @generated
 *
 * C_Personnel_Position_Employee_Page
 *
 */
@Controller
public class C_Personnel_Position_Employee_Page
 extends SC_Personnel_Position_Employee_Page 
{

	@Autowired @Lazy private Service_Admin service_Admin;
	@Autowired @Lazy private Service_Department department_Generate;
	@Autowired @Lazy private Service_Position service_Position;
	
	/**
	 * 分页列表
	 */
	@SuppressWarnings("serial")
	@Action(Page.class)
	@GetMapping(value=Page.RequestMappingValue, consumes=MediaType.APPLICATION_JSON_VALUE)
	public @FomatOutput @ResponseBody Object page(
		HttpServletRequest request, 
		@PathVariable(name=Page.PathVariableName.PositionId) long positionId
	) throws Exception {
		org.springframework.data.domain.Page<TB_Admin> page = service_Admin.distinctPage(
			it->Page.Filter.query(it, request)
				.and(it.positions.contains(new TB_Position(positionId))), 
			Page.Output.page(request), 
			it->Page.Output.sort(it, request, new Page.Output.OrderExtends() {
				@Override public Sort gainPositionName(Direction direction) {
					return sort(direction, it.positions.any().name);
				}
				@Override public Sort gainDepartmentName(Direction direction) {
					return sort(direction, it.departments.any().name);
				}
			})
		);
		//
		return Page.Output.output(
				page.map(it->{
					return new Page.Output.Model(it){
							@Override protected void assign_before() {
								
							}
							@Override public String assignPositionName() {
								return service_Position
									.list(it2->it2.admins.any().eq(it))
									.stream()
									.map(TB_Position::getName)
									.collect(Collectors.toList())
									.toString();
							}
							@Override public String assignDepartmentName() {
								return department_Generate
										.list(it2->it2.admins.any().eq(it))
										.stream()
										.map(TB_Department::getName)
										.collect(Collectors.toList())
										.toString();
							}
						};
				})
			);
	}
	

}


